package com.canyou.redmine.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.canyou.redmine.R;
import com.canyou.redmine.adapter.FragmentAdapter;
import com.canyou.redmine.ui.base.BaseActivity;
import com.canyou.redmine.ui.fragment.IssuesFragment;
import com.canyou.redmine.ui.fragment.ProjectsFragment;
import com.canyou.redmine.ui.fragment.SettingsFragment;
import com.canyou.redmine.ui.fragment.TasksFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面
 *
 * @author ryk
 */
public class MainActivity extends BaseActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> fragments;
    private int[] mTitles = {
            R.string.title_projects, R.string.title_tasks,
            R.string.title_issues, R.string.settings
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }

    private void initUI() {
        tabLayout = findViewById(R.id.tabbar);
        viewPager = findViewById(R.id.viewpager);

        fragments = new ArrayList<>();

        fragments.add(new ProjectsFragment());
        fragments.add(new TasksFragment());
        fragments.add(new IssuesFragment());
        fragments.add(new SettingsFragment());

        viewPager.setAdapter(new FragmentAdapter(this,getSupportFragmentManager(),fragments));
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setTitle(mTitles[tab.getPosition()]);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
